<?php defined('SYSPATH') or die('No direct script access.');
		
	class Model_Services_Profession extends Model implements Jqgrid_GridListener{
	
		public $profession = NULL;
		
		public function __construct($id=false){
			if ($id) $this->profession = $this->get($id);
		}
		
		public function count($criteria = false){
			return ORM::factory("dao_profession")->count_all();
		}
			
		public function getList($criteria = false){
			
			if (!$criteria)	$profession_list = ORM::factory("dao_profession")->find_all()->as_array();
			else{
				if ($criteria->isSearch){
					$profession_list = ORM::factory("dao_profession")
									->where($criteria->getSQLField("professions"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
									->order_by($criteria->sidx,$criteria->sord)
									->limit($criteria->limit)
									->offset($criteria->calcOffset())
									->find_all()
									->as_array();
							
				}else{
					$profession_list = ORM::factory("dao_profession")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
				}
			} 
		
			$dataset = array();
			foreach ($profession_list as $profession){
				$dataset[$profession->id] = array($profession->profession,$profession->description);
			}
			
			return $dataset;
		}
	
		/**
		*
		* get all professions from an id
		* @param int $minId
		* @param int $maxAmount
		* @return array of professions
		*/
		public function getListFromMinID($minId, $maxAmount){
			return ORM::factory("dao_profession")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
		}
		
		public function get($id){
			return ORM::factory("dao_profession")->find($id);
		}
		
		public function getAll(){
			return ORM::factory("dao_profession")->find_all()->as_array();
		}
		
		public function save($id, array $data){
			$this->profession = ($id > 0) ? ORM::factory("dao_profession",$id) : ORM::factory("dao_profession");
			$this->profession->profession = $data['profession'];
			$this->profession->description = $data['description'];
			$this->profession->save();
			return $this->profession->id;
	    }
		
		public function delete($id){
			$profession = ORM::factory("dao_profession",$id);
			$profession->delete();
			return $id;
		}
		
		
		
		/**
		*
		* return an entity profession by name
		* @param string $name
		* @return profession
		*/
		public function getByName($name){
		return ORM::factory("dao_profession")->where("profession","=",$name)->find();
		}
		
	}
	
	
?>
